﻿@*
//------------------------------------------------------------------------------
//  此代码版权声明为全文件覆盖，如有原作者特别声明，会在下方手动补充
//  此代码版权（除特别声明外的代码）归作者本人Diego所有
//  源代码使用协议遵循本仓库的开源协议及附加协议
//  Gitee源代码仓库：https://gitee.com/diego2098/ThingsGateway
//  Github源代码仓库：https://github.com/kimdiego2098/ThingsGateway
//  使用文档：https://diego2098.gitee.io/thingsgateway-docs/
//  QQ群：605534569
//------------------------------------------------------------------------------
*@

@page "/DLT645_2007OverTcp"

@using BlazorComponent;
@using Microsoft.AspNetCore.Components.Web;
@using Microsoft.JSInterop;
@using Masa.Blazor;
@namespace ThingsGateway.Foundation.Demo

<TcpClientPage @ref=_tcpClientPage></TcpClientPage>

<MCard Flat Class="pa-2 my-1" Style="width:100%">
    <div class="mb-4">驱动配置</div>
    <MRow Justify="JustifyTypes.Start" Align="AlignTypes.Center">
        @if (_plc != null)
        {

            <MTextField Class="ma-1" Outlined Style="max-width:100px" Label=@(_plc.Description(x => x.FrameTime)) Dense HideDetails="@("auto")" @bind-Value=@_plc.FrameTime></MTextField>
            <MTextField Class="ma-1" Outlined Style="max-width:100px" Label=@(_plc.Description(x => x.CacheTimeout)) Dense HideDetails="@("auto")" @bind-Value=@_plc.CacheTimeout></MTextField>

            <MTextField Class="ma-1" Outlined Style="max-width:100px" Label=@(_plc.Description(x => x.Station)) Dense HideDetails="@("auto")" @bind-Value=@_plc.Station></MTextField>
            <MTextField Class="ma-1" Outlined Style="max-width:100px" Label=@(_plc.Description(x => x.TimeOut)) Dense HideDetails="@("auto")" @bind-Value=@_plc.TimeOut></MTextField>

            <MTextField Class="ma-1" Outlined Style="max-width:100px" Label=@(_plc.Description(x => x.OperCode)) Dense HideDetails="@("auto")" @bind-Value=@_plc.OperCode></MTextField>
            <MTextField Class="ma-1" Outlined Style="max-width:100px" Label=@(_plc.Description(x => x.Password)) Dense HideDetails="@("auto")" @bind-Value=@_plc.Password></MTextField>


            <MSelect Class="ma-1" Outlined Style="max-width:200px" @bind-Value="_plc.DataFormat" Label="@(_plc.Description(x => x.DataFormat))"
                      Items=@(typeof(DataFormat).GetEnumList())
                      MenuProps="@(props => { props.Auto = true; props.OffsetY = true; })"
                      ItemText=@((u) =>u.Description)
                      ItemValue=@(u =>(DataFormat)u.Value)
                      HideDetails=@("auto") Height="30"
                  Dense>
         </MSelect>

            <MCheckbox Class="ma-1" Style="max-width:200px" Label=@(_plc.Description(x => x.EnableFEHead)) Dense HideDetails="@("auto")" @bind-Value=@_plc.EnableFEHead></MCheckbox>

        }
    </MRow>
</MCard>


<DriverDebugUIPage @ref=_driverDebugUIPage Sections="_sections" ShowDefaultOtherContent=false>
    <OtherContent>
        <MSubheader>
            广播校时
        </MSubheader>
        <MContainer>
            <MRow Dense Align="AlignTypes.Center">

                 <PDateDigitalClockPicker @bind-Value="dateTime" TimeFormat="TimeFormat.Hr24" ViewType="DateTimePickerViewType.Auto" Context="dateContext">
                     <ActivatorContent>
                         <MTextField @bind-Value="dateTime" Style="max-width:200px;" Class="ma-1" Outlined HideDetails=@("auto") Dense Clearable Readonly @attributes="@dateContext.Attrs">
                         </MTextField>
                     </ActivatorContent>
                 </PDateDigitalClockPicker>
                 <MButton Class="ma-1" Color="primary" OnClick="()=>_plc.BroadcastTime(dateTime)">
                     校时
                 </MButton>
             </MRow>
         </MContainer>

         <MSubheader>
             冻结
         </MSubheader>
         <MContainer>
             <MRow Dense Align="AlignTypes.Center">

                 <PDateDigitalClockPicker @bind-Value="dateTime" TimeFormat="TimeFormat.Hr24" ViewType="DateTimePickerViewType.Auto" Context="dateContext">
                     <ActivatorContent>
                         <MTextField @bind-Value="dateTime" Style="max-width:200px;" Class="ma-1" Outlined HideDetails=@("auto") Dense Clearable Readonly @attributes="@dateContext.Attrs">
                         </MTextField>
                     </ActivatorContent>
                 </PDateDigitalClockPicker>
                 <MButton Class="ma-1" Color="primary" OnClick="()=>_plc.FreezeAsync(dateTime)">
                     冻结
                 </MButton>
             </MRow>
         </MContainer>
         <MSubheader>
             读取通信地址
         </MSubheader>
         <MContainer>
             <MRow Dense Align="AlignTypes.Center">

                 <MButton Class="ma-1" Color="primary" OnClick="ReadDeviceStationAsync">
                     读取通信地址
                 </MButton>
             </MRow>
         </MContainer>
         <MSubheader>
             修改波特率
         </MSubheader>
         <MContainer>
             <MRow Dense Align="AlignTypes.Center">

                 <MTextField Class="ma-1" Outlined Style="max-width:200px" Label=波特率 Clearable Dense HideDetails="@("auto")" @bind-Value=baudRate></MTextField>

                 <MButton Class="ma-1" Color="primary" OnClick="WriteBaudRateAsync">
                     修改波特率
                 </MButton>
             </MRow>
         </MContainer>


         <MSubheader>
             更新通信地址
         </MSubheader>
         <MContainer>
             <MRow Dense Align="AlignTypes.Center">

                 <MTextField Class="ma-1" Outlined Style="max-width:200px" Label=通信地址 Clearable Dense HideDetails="@("auto")" @bind-Value=station></MTextField>

                 <MButton Class="ma-1" Color="primary" OnClick="WriteDeviceStationAsync">
                     更新通信地址
                 </MButton>
             </MRow>
         </MContainer>

         <MSubheader>
             修改密码
         </MSubheader>
         <MContainer>
             <MRow Dense Align="AlignTypes.Center">

                 <MTextField Class="ma-1" Outlined Style="max-width:200px" Label=级别(0-9) Clearable Dense HideDetails="@("auto")" @bind-Value=level></MTextField>
                 <MTextField Class="ma-1" Outlined Style="max-width:200px" Label=旧密码 Clearable Dense HideDetails="@("auto")" @bind-Value=oldPassword></MTextField>
                 <MTextField Class="ma-1" Outlined Style="max-width:200px" Label=新密码 Clearable Dense HideDetails="@("auto")" @bind-Value=newPassword></MTextField>

                 <MButton Class="ma-1" Color="primary" OnClick="WritePasswordAsync">
                     修改密码
                 </MButton>
             </MRow>
         </MContainer>



     </OtherContent>
 </DriverDebugUIPage>

 @code {
    private byte level;
    private string oldPassword;
    private string newPassword;
    private async Task WritePasswordAsync()
    {
        var result = await _plc.WritePasswordAsync(level, oldPassword, newPassword);
        if (result.IsSuccess)
        {
            _driverDebugUIPage.Messages.Add((Microsoft.Extensions.Logging.LogLevel.Information, DateTimeExtensions.CurrentDateTime.ToDefaultDateTimeFormat(_driverDebugUIPage.InitTimezone.TimezoneOffset) + " - " + result.Message));
        }
        else
        {
            _driverDebugUIPage.Messages.Add((Microsoft.Extensions.Logging.LogLevel.Warning, DateTimeExtensions.CurrentDateTime.ToDefaultDateTimeFormat(_driverDebugUIPage.InitTimezone.TimezoneOffset) + " - " + result.Message));
        }
    }


    private string station;
    private async Task WriteDeviceStationAsync()
    {
        var result = await _plc.WriteDeviceStationAsync(station);
        if (result.IsSuccess)
        {
            _driverDebugUIPage.Messages.Add((Microsoft.Extensions.Logging.LogLevel.Information, DateTimeExtensions.CurrentDateTime.ToDefaultDateTimeFormat(_driverDebugUIPage.InitTimezone.TimezoneOffset) + " - " + result.Message));
        }
        else
        {
            _driverDebugUIPage.Messages.Add((Microsoft.Extensions.Logging.LogLevel.Warning, DateTimeExtensions.CurrentDateTime.ToDefaultDateTimeFormat(_driverDebugUIPage.InitTimezone.TimezoneOffset) + " - " + result.Message));
        }
    }

    private int baudRate;
    private async Task WriteBaudRateAsync()
    {
        var result = await _plc.WriteBaudRateAsync(baudRate);
        if (result.IsSuccess)
        {
            _driverDebugUIPage.Messages.Add((Microsoft.Extensions.Logging.LogLevel.Information, DateTimeExtensions.CurrentDateTime.ToDefaultDateTimeFormat(_driverDebugUIPage.InitTimezone.TimezoneOffset) + " - " + result.Message));
        }
        else
        {
            _driverDebugUIPage.Messages.Add((Microsoft.Extensions.Logging.LogLevel.Warning, DateTimeExtensions.CurrentDateTime.ToDefaultDateTimeFormat(_driverDebugUIPage.InitTimezone.TimezoneOffset) + " - " + result.Message));
        }
    }

    private async Task ReadDeviceStationAsync()
    {
        var result = await _plc.ReadDeviceStationAsync();
        if (result.IsSuccess)
        {
            _driverDebugUIPage.Messages.Add((Microsoft.Extensions.Logging.LogLevel.Information, DateTimeExtensions.CurrentDateTime.ToDefaultDateTimeFormat(_driverDebugUIPage.InitTimezone.TimezoneOffset) + " - " + result.Content));
        }
        else
        {
            _driverDebugUIPage.Messages.Add((Microsoft.Extensions.Logging.LogLevel.Warning, DateTimeExtensions.CurrentDateTime.ToDefaultDateTimeFormat(_driverDebugUIPage.InitTimezone.TimezoneOffset) + " - " + result.Message));
        }
    }
    private DateTime dateTime;
}

@code {
    private readonly List<(string Code, string Language)> _sections = new();

}